{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f766b322-a021-4de7-8ac5-de59a3489577",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的所有完数有：\n",
      "[6, 28, 496]\n"
     ]
    }
   ],
   "source": [
    "def find_factors(n):\n",
    "    \"\"\"找出n的所有因子（除了n本身）\"\"\"\n",
    "    factors = []\n",
    "    for i in range(1, n):\n",
    "        if n % i == 0:\n",
    "            factors.append(i)\n",
    "    return factors\n",
    "\n",
    "def is_perfect_number(n):\n",
    "    \"\"\"判断n是否为完数\"\"\"\n",
    "    factors = find_factors(n)\n",
    "    return sum(factors) == n\n",
    "\n",
    "# 主程序\n",
    "perfect_numbers = []\n",
    "for num in range(1, 1001):\n",
    "    if is_perfect_number(num):\n",
    "        perfect_numbers.append(num)\n",
    "\n",
    "print(\"1000以内的所有完数有：\")\n",
    "print(perfect_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "749a09e8-7748-4ad1-894f-b41bab3f6a42",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
